﻿using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;

namespace SPS.ImageAttachementFieldType.ImageAttachementFieldTypeEventReceiver
{
    /// <summary>
    /// List Item Events
    /// </summary>
    public class ImageAttachementFieldTypeEventReceiver : SPItemEventReceiver
    {
        #region Private Memebers
        string _defaultImage = "/_layouts/SPSImageAttachementFieldType/images/DefaultImage.gif";
        string _fieldName = "Picture";
        #endregion
        
        /// <summary>
        /// An item was added.
        /// </summary>
        public override void ItemAdded(SPItemEventProperties properties)
        {
            SPFieldUrlValue urlValue = null;
            try
            {
                base.ItemAdded(properties);
                if (properties.List.Fields.ContainsField(_fieldName) && properties.List.Fields.GetFieldByInternalName(_fieldName).TypeAsString == "ImageAttachementField")
                //if (properties.List.Fields.ContainsField(_fieldName) && properties.List.Fields[_fieldName].TypeAsString == "ImageAttachementField")
                {
                    if (properties.ListItem[_fieldName] != null)
                    {
                        urlValue = new SPFieldUrlValue(properties.ListItem[_fieldName].ToString());
                        string fileName = urlValue.Url;
                        fileName = fileName.Replace("http://", "");
                        urlValue.Url = properties.ListItem.Attachments.UrlPrefix + fileName;
                    }
                    else
                    {
                        urlValue = new SPFieldUrlValue();
                        urlValue.Url = properties.Web.Url.TrimEnd("/".ToCharArray()) + _defaultImage;
                    }
                    properties.ListItem[_fieldName] = urlValue;

                    EventFiringEnabled = false;
                    properties.ListItem.SystemUpdate();
                    EventFiringEnabled = true;

                }
            }
            catch (Exception ex)
            {
                urlValue = new SPFieldUrlValue();
                urlValue.Url = properties.Web.Url.TrimEnd("/".ToCharArray()) + _defaultImage;

                properties.ListItem[_fieldName] = urlValue;

                EventFiringEnabled = false;
                properties.ListItem.SystemUpdate();
                EventFiringEnabled = true;

                //Logging.GetInstance().LogException(ex);
            }

        }

        /// <summary>
        /// An item was updated.
        /// </summary>
        public override void ItemUpdated(SPItemEventProperties properties)
        {
            SPFieldUrlValue urlValue = null;
            try
            {

                base.ItemUpdated(properties);
                if (properties.List.Fields.ContainsField(_fieldName) && properties.List.Fields.GetFieldByInternalName(_fieldName).TypeAsString == "ImageAttachementField")
                //if (properties.List.Fields.ContainsField(_fieldName) && properties.List.Fields[_fieldName].TypeAsString == "ImageAttachementField")
                {
                    if (properties.ListItem[_fieldName] != null)
                    {
                        urlValue = new SPFieldUrlValue(properties.ListItem[_fieldName].ToString());
                        string fileName = urlValue.Url;

                        if (string.IsNullOrEmpty(fileName))
                        {
                            urlValue.Url = properties.Web.Url.TrimEnd("/".ToCharArray()) + _defaultImage;
                        }
                        else if (!fileName.Contains(properties.ListItem.Attachments.UrlPrefix) && !fileName.Contains(_defaultImage)
                            && !fileName.Replace("http://", "").Contains("/"))
                        {
                            fileName = fileName.Replace("http://", "");
                            urlValue.Url = properties.ListItem.Attachments.UrlPrefix + fileName;
                        }
                    }
                    else
                    {
                        urlValue = new SPFieldUrlValue();
                        urlValue.Url = properties.Web.Url.TrimEnd("/".ToCharArray()) + _defaultImage;
                    }
                    properties.ListItem[_fieldName] = urlValue;

                    EventFiringEnabled = false;
                    properties.ListItem.SystemUpdate();
                    EventFiringEnabled = true;
                }
            }
            catch (Exception ex)
            {
                urlValue = new SPFieldUrlValue();
                urlValue.Url = properties.Web.Url.TrimEnd("/".ToCharArray()) + _defaultImage;

                properties.ListItem[_fieldName] = urlValue;

                EventFiringEnabled = false;
                properties.ListItem.SystemUpdate();
                EventFiringEnabled = true;

                //Logging.GetInstance().LogException(ex);
            }

        }



    }
}
